package com.example.recuitactivity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.recuitactivity.Builder.RecruitInfoConcreteBuilder1;
import com.example.recuitactivity.Builder.RecruitInfoConcreteBuilder2;
import com.example.recuitactivity.Builder.RecruitInfoDirector;
import com.example.recuitactivity.bean.Candidate;
import com.example.recuitactivity.bean.RecuitInfo;
import com.example.recuitactivity.helper.DbOpenHelper;

import java.util.List;

public class EditRecuitInfoActivity extends AppCompatActivity {
    private Button btn_save,btn_update,btn_delete;
    private EditText et_name,et_salary,et_edubg,et_scale,et_location,et_detail;
    private TextView tv_company;
    private TextView tv_num_delivered,tv_num_collected,tv_num_scan;
    private ImageView iv_num_delivered,iv_num_collected,iv_num_scan;
    private DbOpenHelper dbOpenHelper;
    private long itemId;

    private View.OnClickListener listener=new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch(view.getId()){
                case R.id.btn_save_recruit_info:
                    new AlertDialog.Builder(EditRecuitInfoActivity.this)
                            .setTitle("提示")
                            .setMessage("确定发布该岗位信息吗？")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    saveInfo();
                                    Toast.makeText(EditRecuitInfoActivity.this,"发布成功",Toast.LENGTH_SHORT).show();
                                    finish();
                                }
                            })
                            .setNegativeButton("取消",null)
                            .show();
                    break;
                case R.id.btn_update_recruit_info:
                    updateInfo();
                    sendNotification();
                    Toast.makeText(EditRecuitInfoActivity.this,"修改成功",Toast.LENGTH_SHORT).show();
                    finish();
                    break;
                case R.id.btn_delete_recruit_info:
                    new AlertDialog.Builder(EditRecuitInfoActivity.this)
                            .setTitle("提示")
                            .setMessage("确定删除该岗位信息吗？")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    deleteInfo();
                                    Toast.makeText(EditRecuitInfoActivity.this,"删除成功",Toast.LENGTH_SHORT).show();
                                    finish();
                                }
                            })
                            .setNegativeButton("取消",null)
                            .show();
                    break;
            }
        }
    };

    public void initUI(){
        btn_save=(Button) findViewById(R.id.btn_save_recruit_info);
        btn_update=(Button) findViewById(R.id.btn_update_recruit_info);
        btn_delete=(Button) findViewById(R.id.btn_delete_recruit_info);
        et_name=(EditText) findViewById(R.id.et_recruit_info_name);
        et_salary=(EditText) findViewById(R.id.et_recruit_info_salary);
        et_edubg=(EditText) findViewById(R.id.et_recruit_info_edubgrequire);
        et_scale=(EditText) findViewById(R.id.et_recruit_info_scale);
        et_location=(EditText) findViewById(R.id.et_recruit_info_location);
        et_detail=(EditText) findViewById(R.id.et_recruit_info_detail);
        tv_company=(TextView) findViewById(R.id.tv_recruit_info_company);
        tv_num_delivered=(TextView) findViewById(R.id.tv_num_recruit_delivered);
        tv_num_collected=(TextView) findViewById(R.id.tv_num_recruit_collected);
        tv_num_scan=(TextView) findViewById(R.id.tv_num_recruit_scan);
        iv_num_delivered=(ImageView) findViewById(R.id.iv_num_recruit_delivered);
        iv_num_collected=(ImageView) findViewById(R.id.iv_num_recruit_collected);
        iv_num_scan=(ImageView) findViewById(R.id.iv_num_recruit_scan);


        btn_save.setOnClickListener(listener);
        btn_update.setOnClickListener(listener);
        btn_delete.setOnClickListener(listener);
    }

    public void initDates(){
        dbOpenHelper=new DbOpenHelper(this);
        String action=getIntent().getAction() ;
        if(action.equals("createRecruitInfo")){
            btn_update.setVisibility(View.INVISIBLE);
            btn_delete.setVisibility(View.INVISIBLE);
            iv_num_delivered.setVisibility(View.INVISIBLE);
            iv_num_collected.setVisibility(View.INVISIBLE);
            iv_num_scan.setVisibility(View.INVISIBLE);
            tv_num_delivered.setVisibility(View.INVISIBLE);
            tv_num_collected.setVisibility(View.INVISIBLE);
            tv_num_scan.setVisibility(View.INVISIBLE);

            String company=dbOpenHelper.getEnterpriseCompanyByUsername(getIntent().getExtras().getString("username"));
            tv_company.setText(company);
        }else if(action.equals("updateRecruitInfo")){
            btn_save.setVisibility(View.INVISIBLE);
            Bundle bundle=getIntent().getExtras();
            et_name.setText(bundle.getString("recruit_name"));
            et_salary.setText(bundle.getString("salary"));
            et_edubg.setText(bundle.getString("edubg_request"));
            tv_company.setText(bundle.getString("enterprise_name"));
            et_scale.setText(bundle.getString("scale"));
            et_location.setText(bundle.getString("location"));
            et_detail.setText(bundle.getString("detail"));
            itemId=bundle.getLong("id");

            int deliveredNumber=dbOpenHelper.getDeliveredNumByPid(itemId);
            int collectedNumber=dbOpenHelper.getCollectsNumByPid(itemId);
            long scanNumber=dbOpenHelper.getTotalBrowsesNumByPid(itemId);
            tv_num_delivered.setText(String.valueOf(deliveredNumber));
            tv_num_collected.setText(String.valueOf(collectedNumber));
            tv_num_scan.setText(String.valueOf(scanNumber));
        }
    }

    public void saveInfo(){
        String username=et_name.getText().toString();
        String company=tv_company.getText().toString();
        String salary=et_salary.getText().toString();
        String edubg=et_edubg.getText().toString();
        String scale=et_scale.getText().toString();
        String location=et_location.getText().toString();
        String detail=et_detail.getText().toString();

        RecruitInfoDirector director=new RecruitInfoDirector(new RecruitInfoConcreteBuilder1());
        RecuitInfo recuitInfo=director.construct(-1,username,company,salary,edubg,location,detail,scale);
        dbOpenHelper.insertPost(recuitInfo);
    }

    public void updateInfo(){
        String username=et_name.getText().toString();
        String company=tv_company.getText().toString();
        String salary=et_salary.getText().toString();
        String edubg=et_edubg.getText().toString();
        String scale=et_scale.getText().toString();
        String location=et_location.getText().toString();
        String detail=et_detail.getText().toString();

        RecruitInfoDirector director=new RecruitInfoDirector(new RecruitInfoConcreteBuilder2());
        RecuitInfo recuitInfo=director.construct(itemId,username,company,salary,edubg,location,detail,scale);
        dbOpenHelper.updatePost(recuitInfo);
    }

    public void deleteInfo(){
        dbOpenHelper.deletePost(itemId);
    }

    public void sendNotification(){
        RecuitInfo recuitInfo=dbOpenHelper.getRecruitInfoById(itemId);
        List<Long> cidList=dbOpenHelper.getCollectsCidByPid(itemId);
        for(int i=0;i<cidList.size();i++){
            Candidate candidate=dbOpenHelper.getCandidateByCId(cidList.get(i));
            recuitInfo.attachCandidates(candidate);
        }
        recuitInfo.notifyToAllCandidates(dbOpenHelper);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_recuit_info);
        ActionBar actionBar=getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        initUI();
        initDates();
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if(item.getItemId()==android.R.id.home){
            finish();
            return true;
        }
        return false;

    }
}